package com.backend.controller;


import com.backend.common.*;
import com.backend.entity.RoomCategory;
import com.backend.exception.BusinessException;
import com.backend.exception.ThrowUtils;
import com.backend.service.RoomCategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/roomCategory")
public class RoomCategoryController {

    @Resource
    private RoomCategoryService roomCategoryService;

    /**
     * 分页
     * @param pageRequest
     * @return
     */
    @PostMapping("/page")
    public BaseResponse<PageResponse> page(@RequestBody PageRequest pageRequest) {
        System.out.println(pageRequest);
        PageResponse result = roomCategoryService.page(pageRequest);
        return ResultUtils.ok(result);
    }

    /**
     * 添加
     */
    @PostMapping("/add")
    public BaseResponse<?> add(@RequestBody RoomCategory roomCategory, HttpServletRequest request) {
        if (roomCategory == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        boolean result = roomCategoryService.save(roomCategory);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.ok(null);
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> delete(@RequestBody Long[] ids, HttpServletRequest request) {
        roomCategoryService.removeByIds(Arrays.asList(ids));
        return ResultUtils.ok(null);
    }

    /**
     * 更新
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> update(@RequestBody RoomCategory roomCategory, HttpServletRequest request) {
        if (roomCategory == null || roomCategory.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = roomCategoryService.updateById(roomCategory);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.ok(null);
    }

    @GetMapping("/lists")
    public BaseResponse<List<RoomCategory>> lists() {
        return ResultUtils.ok(roomCategoryService.list());
    }


    @PostMapping("/detail/{id}")
    public BaseResponse<RoomCategory> detail(@PathVariable("id")String id) {
        RoomCategory roomCategory = roomCategoryService.getById(id);
        System.out.println(id.getClass().toString());
        return ResultUtils.ok(roomCategory);
    }
}
